package base.email;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lxit.cms.entity.main.CmsUser;
import com.lxit.cms.manager.main.CmsUserMng;
import com.lxitedu.tool.SMSTool;

/**
 * Servlet implementation class SendEmailAndInfo
 */
@Controller
public class SendSMSServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SendSMSServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		SMSTool.init();
	}

	@Override
	public void destroy() {

		SMSTool.cleanUp();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String result = "已经发送";
		
		setEncoding(request, response);
		
		String phone = request.getParameter("phone");
		if (phone!=null) {
			sendMemberEms(request, phone);
		} else {
			result = sendWanggeManager(request, response, result);
		}
		
		returnJson(response, result);
	}

	private void setEncoding(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	private void returnJson(HttpServletResponse response, String result) {
		try {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			response.getWriter().print("{result:'" + result + "'}");
		} catch (IOException e) {
			System.out.println("返回失败");
		}
	}

	private void sendMemberEms(HttpServletRequest request, String phone) {
		String content = request.getParameter("reply");
		SMSTool.sendMessage(phone, "意见处理反馈:"+content+"！感谢您的宝贵意见！请在观澜虹桥网上对我们的回复进行评分。");
	}

	private String sendWanggeManager(HttpServletRequest request,
			HttpServletResponse response, String result)
			throws UnsupportedEncodingException {
		setServlet();// *********得到Spring容器***********
		String userId = request.getParameter("userId");
		System.out.println("SendSMSServlet.doPost() :userId:"+userId);
		if (userId.equals("") || userId == null) {
			userId = "0";
			result = "发送失败";
		}
		String phone = null;
		CmsUser cmsUser = getCmsUserMng().findById(Integer.parseInt(userId));
		if (cmsUser != null) {
			phone = cmsUser.getMoblie();//fixme 接收信息党员的email
			System.out.println("SendSMSServlet.doPost() phone:"+phone);
		}
		String title = request.getParameter("title");// 信息标题
		String content = getReturnManagerContent(request);// 信息内容
		
		System.out.println("SendEmailAndInfo.doPost() content:" + content);
		System.out.println("SendEmailAndInfo.doPost() title:" + title);

		try {
			SMSTool.sendMessage(phone, content);
		} catch (Exception e1) {
			result="数据库连接问题,发送失败";
		}
		return result;
	}

	private String getReturnManagerContent(HttpServletRequest request) {
		String content = request.getParameter("content");
		content="观澜虹桥网提示您有一个新的消息："+content+"，请及时处理相关问题，并在三天内登陆账号进行回复。谢谢！";
		return content;
	}

	

	public void setServlet() {
		ServletContext servletContext = getServletContext();
		setWac(WebApplicationContextUtils
				.getRequiredWebApplicationContext(servletContext));
		setCmsUserMng((CmsUserMng) getWac().getBean("cmsUserMng"));
	}

	private WebApplicationContext wac;

	public void setCmsUserMng(CmsUserMng cmsUserMng) {
		this.cmsUserMng = cmsUserMng;
	}

	public CmsUserMng getCmsUserMng() {
		return cmsUserMng;
	}

	public void setWac(WebApplicationContext wac) {
		this.wac = wac;
	}

	public WebApplicationContext getWac() {
		return wac;
	}

	private CmsUserMng cmsUserMng;

}
